package net.gyapp.markim.module.contact.adapter;

import android.text.TextUtils;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import net.gyapp.markim.R;
import net.gyapp.markim.core.entity.GroupEntity;
import net.gyapp.markim.core.entity.MemberEntity;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.contact.vo.ContactHeaderEntity;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class ContactsAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseRecyclerHolder> {

    public final static int TYPE_CONTACT_HEADER = 3001;
    public final static int TYPE_CONTACT_USER = 3002;
    public final static int TYPE_CONTACT_GROUP = 3003;

    public ContactsAdapter() {
        super(null);
        addItemType(TYPE_CONTACT_HEADER, R.layout.item_contact_cate);
        addItemType(TYPE_CONTACT_USER, R.layout.item_contact_content);
        addItemType(TYPE_CONTACT_GROUP, R.layout.item_contact_content);
    }


    public void sort(List<MemberEntity> friendList) {
        // 排序
        Collections.sort(friendList, new Comparator<MemberEntity>() {
            @Override
            public int compare(MemberEntity lhs, MemberEntity rhs) {
                if (lhs.firstLetter.equals(rhs.firstLetter)) {
                    return lhs.memberId.compareTo(rhs.memberId);
                } else {
                    if ("#".equals(lhs.firstLetter)) {
                        return 1;
                    } else if ("#".equals(rhs.firstLetter)) {
                        return -1;
                    }
                    return lhs.firstLetter.compareTo(rhs.firstLetter);
                }
            }
        });
    }

    public int getLetterPosition(String letter) {
        int position = -1;
        for (int i = 0; i < mData.size(); i++) {
            MultiItemEntity entity = mData.get(i);
            if (entity instanceof ContactHeaderEntity) {
                if (((ContactHeaderEntity) entity).firstLetter.equalsIgnoreCase(letter)) {
                    return i;
                }
            }
        }
        return position;
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_CONTACT_HEADER:
                convertHeader(helper, (ContactHeaderEntity) item);
                break;
            case TYPE_CONTACT_USER:
                convertUser(helper, (MemberEntity) item);
                break;
            case TYPE_CONTACT_GROUP:
                convertGroup(helper, (GroupEntity) item);
                break;
        }
    }

    private void convertUser(BaseRecyclerHolder helper, MemberEntity item) {
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, R.drawable.placeholder_header);
        helper.setText(R.id.tv_name, TextUtils.isEmpty(item.remark) ? item.nick : item.remark);
    }

    private void convertGroup(BaseRecyclerHolder helper, GroupEntity item) {
        helper.setText(R.id.tv_name, item.name);
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, R.drawable.placeholder_group_header);
    }

    private void convertHeader(BaseRecyclerHolder helper, ContactHeaderEntity item) {
        helper.setText(R.id.tv_cate, item.name);
    }

}
